package com.example.springboot.controller;

import cn.hutool.core.collection.CollUtil;
import com.example.springboot.common.Result;
import com.example.springboot.controller.dto.LoginDTO;
import com.example.springboot.controller.request.AdminPageRequest;
import com.example.springboot.controller.request.CategoryPageRequest;
import com.example.springboot.controller.request.LoginRequest;
import com.example.springboot.controller.request.PasswordRequest;
import com.example.springboot.entity.Category;
import com.example.springboot.service.IAdminService;
import com.example.springboot.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author:Domen
 * @Date:2023/11/7 16:36
 */

@CrossOrigin
@RestController
@Controller
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    ICategoryService categoryService;

    @PostMapping("/save")
    public Result save(@RequestBody Category obj){
        categoryService.save(obj);
        return Result.success();
    }

    @PutMapping("/update")
    public Result update(@RequestBody Category obj){
        categoryService.update(obj);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        categoryService.deleteById(id);
        return Result.success();
    }

    @GetMapping("/list")
    public Result list(){
       List<Category> list = categoryService.list();
       return Result.success(list);        //包装一下，使返回的结果更加规范
    }

    @GetMapping("/tree")
    public Result listTree(){
        List<Category> list = categoryService.list();

        return Result.success(createTree(null,list));        //包装一下，使返回的结果更加规范
    }

    //递归实现递归树
    private List<Category> createTree(Integer pid, List<Category> categories) {
        List<Category> treeList = new ArrayList<>();
        for (Category category : categories) {
            if(pid == null){
                if(category.getPid() == null){   //这个就是第一节点
                    treeList.add(category);
                    category.setChildren(createTree(category.getId(), categories));
                }
            }else{  //判断其他节点
                if (pid.equals(category.getPid())) {
                    treeList.add(category);
                    category.setChildren(createTree(category.getId(), categories));
                }
            }
            //递归出口
            if (CollUtil.isEmpty(category.getChildren())) {
                category.setChildren(null);
            }
        }
        return treeList;
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        Category obj = categoryService.getById(id);
        return Result.success(obj);
    }

    @GetMapping("/page")
    public Result page(CategoryPageRequest pageRequest){
        return Result.success(categoryService.page(pageRequest));        //包装一下，使返回的结果更加规范
    }

}
